﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Workflow;
using Xrm.Framework.Test.Unit.Fakes;
using Xrm.Solutions.FrameworkSample.Workflow;
using Xrm.Solutions.FrameworkSample.Common.Entities;
using Microsoft.Xrm.Sdk.Messages.Fakes;
using Microsoft.QualityTools.Testing.Fakes;


namespace Xrm.Solutions.FrameworkSample.XrmUnitTests.WFActivities
{
    [TestClass]
    public class TestOpportunitiesTotal : WFActivityUnitTest
    {
        #region Instance Variables

        //TODO: Declare your variables for setup and verification

        #endregion

        #region Setup

        protected override Activity SetupActivity()
        {
            //TODO: Setup your Stubs & Shim

            base.OrganizationServiceStub.ExecuteOrganizationRequest = (request) =>
            {
                if (request is RetrieveMultipleRequest)
                {
                    EntityCollection opps = new EntityCollection();
                    opps.Entities.Add(new Opportunity() { ActualValue = new Money(100) });
                    opps.Entities.Add(new Opportunity() { ActualValue = new Money(200) });
                    opps.Entities.Add(new Opportunity() { ActualValue = new Money(300) });

                    ShimRetrieveMultipleResponse res = new Microsoft.Xrm.Sdk.Messages.Fakes.ShimRetrieveMultipleResponse()
                    {
                        EntityCollectionGet = () => { return opps; }
                    };

                    return res;
                }
                throw new NotImplementedException();
            };

            base.Inputs.Add("AccountReference",
                new EntityReference("account", Guid.NewGuid()));

            //TODO: Create your Activity
            Activity activity = new TotalOpportunities();

            return activity;
        }

        #endregion

        #region Test

        [TestMethod]
        public void RunTestOpportunityTotal()
        {
            using (ShimsContext.Create())
            {
                base.Test();
            }
        }

        #endregion

        #region Verify

        protected override void Verify()
        {
            Assert.IsNull(Error);

            Money actualTotal = this.Outputs["Total"] as Money;
            decimal expectedTotal = 600;

            Assert.IsNotNull(actualTotal);
            Assert.AreEqual(expectedTotal, actualTotal.Value);
        }

        #endregion
    }
}
